/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goodsinfo.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.orderlist.entity.Orderlist;
import com.jeesite.modules.orderlist.service.OrderlistService;
import com.jeesite.modules.sys.utils.UserUtils;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.goodsinfo.entity.Goodsinfo;
import com.jeesite.modules.goodsinfo.service.GoodsinfoService;

/**
 * js_goodsinfoController
 * @author 达芬奇
 * @version 2019-10-05
 */
@Controller
@RequestMapping(value = "${adminPath}/goodsinfo/goodsinfo")
public class GoodsinfoController extends BaseController {

	@Autowired
	private GoodsinfoService goodsinfoService;

	@Autowired
	private OrderlistService orderlistService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Goodsinfo get(String id, boolean isNewRecord) {
		return goodsinfoService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("goodsinfo:goodsinfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(Goodsinfo goodsinfo, Model model) {
		model.addAttribute("goodsinfo", goodsinfo);
		return "modules/goodsinfo/goodsinfoList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("goodsinfo:goodsinfo:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Goodsinfo> listData(Goodsinfo goodsinfo, HttpServletRequest request, HttpServletResponse response) {
		goodsinfo.setPage(new Page<>(request, response));
		Page<Goodsinfo> page = goodsinfoService.findPage(goodsinfo);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("goodsinfo:goodsinfo:view")
	@RequestMapping(value = "form")
	public String form(Goodsinfo goodsinfo, Model model) {
		model.addAttribute("goodsinfo", goodsinfo);
		return "modules/goodsinfo/goodsinfoForm";
	}

	/**
	 * 保存商品信息管理
	 */
	@RequiresPermissions("goodsinfo:goodsinfo:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Goodsinfo goodsinfo) {
		Orderlist orderlist = new Orderlist();
		orderlist.setAddress(goodsinfo.getAddress());
		orderlist.setGoodid(goodsinfo.getId());
		orderlist.setGoodnum((int) Math.ceil(goodsinfo.getGoodsnum()));
		orderlist.setGoodname(goodsinfo.getGoodsdes());
		orderlist.setGoodstotalprice(1.5*goodsinfo.getGoodsnum());
		orderlist.setUid(UserUtils.getUser().getId());
		orderlist.setState(0);
		orderlistService.save(orderlist);
		Goodsinfo goodsinfo1 = goodsinfoService.get(goodsinfo.getId());
		if(goodsinfo1.getGoodsnum()<goodsinfo.getGoodsnum()){
			return renderResult(Global.FALSE, text("购买商品失败！"));
		}
		goodsinfo1.setGoodsnum(goodsinfo1.getGoodsnum()-goodsinfo.getGoodsnum());
		goodsinfoService.save(goodsinfo1);
		return renderResult(Global.TRUE, text("购买商品成功！"));
	}
	
	/**
	 * 删除商品信息管理
	 */
	@RequiresPermissions("goodsinfo:goodsinfo:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Goodsinfo goodsinfo) {
		goodsinfoService.delete(goodsinfo);
		return renderResult(Global.TRUE, text("删除商品信息管理成功！"));
	}
	
}